var models = require('../db'); // 数据库链接信息
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');// 导入数据模块

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();
var jsonWrite = function(res, ret) {
    if(typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        });
    } else {
        res.json(ret);
    }
};

// 登录
router.post('/login', (req, res) => {
    var params = req.body;
    var sql = " select * from user where username = ? and password = ?";
    console.log(params);
    conn.query(sql, [params.username, params.password], function(err, result) {
      if (err) {
        console.log(err);
      }
      if (result) {
        console.log(result.length);
        if(result.length === 0 ){
          console.log("不存在该用户");
          jsonWrite(res, result);
        }
        else {
          console.log("存在该用户");
          jsonWrite(res, result);
        }
      }
    })
})
// 增加用户接口 (username, password, sex, birthday, school, college, studentNumber) 
router.post('/addUser', (req, res) => {
    var sql = "insert into user (username, password, sex, birthday, school, college, studentNumber, register_time) VALUES (?,?,?,?,?,?,?,NOW())";
    var params = req.body;
    console.log(params);
    conn.query(sql, [
                    params.username, 
                    params.password,
                    params.sex,
                    params.birthday ,
                    params.school,
                    params.college,
                    params.studentNumber
                ], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});

// 查询所有用户接口
router.post('/queryUser', (req, res) => {
    var sql = "select * from user;"
    var params = req.body
    console.log(params);
    conn.query(sql, [
                    params.username,
                    params.sex,
                    params.birthday,
                    params.school,
                    params.college,
                    params.studentNumber,
                    params.register_time
    ], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            // console.log('=======================');
            // console.log(res);
            jsonWrite(res, result);
        }
    })
})

// 人流量统计数据
router.post('/queryData', (req, res) => {
    var sql = "select * from visitorsFlowrate;"
    var params = req.body
    console.log(params);
    conn.query(sql, [
        params.id,
        params.count,
        params.time
    ], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            console.log('---------------------------');
            // console.log(res);
            console.log(result);
            jsonWrite(res, result);
        }
    })
})

module.exports = router;